package com.wondertek.service.mapper.business;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.service.dto.app.req.RentalListReq;
import com.wondertek.service.dto.app.res.RentalListRes;
import com.wondertek.service.dto.app.res.RentalRoomRes;
import com.wondertek.service.dto.app.res.RentalSiteRes;
import com.wondertek.service.dto.business.req.RentalDeviceAddReq;
import com.wondertek.service.dto.business.req.RentalFindRdReq;
import com.wondertek.service.dto.business.req.RentalRoomDeviceReq;
import com.wondertek.service.dto.business.res.RentalKvRes;
import com.wondertek.service.dto.business.res.RentalRoomDeviceRes;
import com.wondertek.service.dto.business.res.RoomAllRes;
import com.wondertek.service.dto.business.res.SiteAllRes;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.entity.business.RentalDevicePo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangyu
 */
public interface RentalMapper extends BaseMapper<RentalDevicePo> {


    /**
     * 查询所有点位
     * @return
     */
    @Select("<script> select id value,site_name name " +
            "from site where is_delete = 0 " +
            "<if test='userId != null and userId !=\"\" '>" +
            " and id in (select site_id from user_site where user_id=#{userId})" +
            "</if>"+
            "</script>")
    List<RentalKvRes> findAllSite(@Param("userId") String userId);

    /**
     * 根据点位id查询该点位下还未租赁的设备  (改:  查所有)
     * @param siteId
     * @param rentalType 1:分区 2:设备
     * @return
     */
    @Select("<script> select d.id as value,device_name as name from device d left join device_type_detail dt on d.device_type_id = dt.id where is_delete = 0 <if test='rentalType == 1'> and dt.category = 2 </if>  <if test=' rentalType ==2 '> and dt.category != 2 </if> and site_id = #{siteId} </script>")
    List<RentalKvRes> findDeviceBySiteId(@Param("siteId") String siteId, @Param("rentalType") Integer rentalType);

    /**
     * 根据设备id查询该设备下还未租赁的分区 (改: 查所有)
     * @param deviceId
     * @return
     */
    @Select("select id as value,room_name as name from room r where is_delete = 0 and device_id = #{deviceId} ")
    List<RentalKvRes> findRoomByDeviceId(@Param("deviceId") String deviceId);

    /**
     * 查询站点下还未租赁的分区/设备  (改了 设备租赁多对多 被租赁的要查)
     * @param req
     * @return
     */
    @Select("<script> select * from ( select d.device_name,d.id as device_id,r.id as room_id,r.room_name from room r right join device d on r.device_id = d.id and r.is_delete = 0  where d.site_id = #{siteId} ) res  <if test='type ==1'> where res.room_id is not null </if> <if test='type==2'> where res.room_id is null </if> </script>")
    List<RoomAllRes> findAllRoomBySiteId(RentalFindRdReq req);

//    /**
//     * 根据app用户id查询站点
//     * @param consumerId
//     * @return
//     */
//    @Select("<script> SELECT d.site_id,( SELECT site_name FROM site WHERE id = d.site_id ) site_name FROM device d, rental_device r WHERE d.id = r.device_id AND r.consumer_id = #{consumerId} </script>")
//    List<RentalSiteRes> findSiteByConsumerId(@Param("consumerId") String consumerId);
//
//    @Select("<script> SELECT * FROM ( SELECT ( SELECT site_id FROM device d WHERE d.id = r.device_id ) site_id, room_id,( SELECT room_name FROM room m WHERE m.id = r.room_id ) room_name FROM rental_device r ) v WHERE v.site_id = #{siteId} </script>")
//    List<RentalRoomRes> findRoomBySiteId(@Param("siteId") String siteId);


    /**
     * 查询租赁管理
     * @param req
     * @param page
     * @return
     */
    @Select("<script> select id as consumer_id,email,username, nickname,(select count(1) from rental_device  where is_delete = 0 and consumer_id =  c.id and end_date > now() ) rentalStatus,create_time from consumer c where 1=1  <if test='req.searchValue != null and req.searchValue !=\"\" '> and nickname like concat('%',#{req.searchValue},'%') or email like concat('%',#{req.searchValue},'%') </if> </script>")
    Page<RentalListRes> findRentalList(RentalListReq req, Page page);

    /**
     * 租赁管理导出
     * @param req
     * @return
     */
    @Select("<script> select email, nickname,case (select count(1) from rental_device  where is_delete = 0 and consumer_id =  c.id and end_date > now() ) when 0 then 'not leased' else 'under lease' end rentalStatus,create_time from consumer c where 1=1 <if test='req.ids != null and req.ids.size > 0'> and c.id in <foreach collection='req.ids' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if>  <if test='req.searchValue != null and req.searchValue !=\"\" '> and nickname like concat('%',#{req.searchValue},'%') or email like concat('%',#{req.searchValue},'%') </if> </script>")
    List<RentalListRes> findRentalListExport(@Param("req") RentalListReq req);

    /**
     * 查询分区租赁、设备租赁
     * @return
     */
    @Select("<script>select * from ( select d.sn_code,r.id as rentalId,d.site_id,(select site_name from site s where s.id=site_id) site_name,device_id,device_name,room_id,(select room_name from room where id = r.room_id) room_name,(select count(1) from accessory a where is_delete = 0 and a.room_id = r.room_id) accessoryNum,start_date,end_date from rental_device r left join device d on r.device_id = d.id where  r.is_delete = 0 <if test='req.consumerId != null and req.consumerId !=\"\"'> and consumer_id = #{req.consumerId} </if> <if test='req.searchType ==1'> and room_id is not null and room_id != '' </if> <if test='req.searchType ==2'> and (room_id is null or room_id = '') </if> ) rs where 1=1 <if test='req.leaseStatus == 1'> and end_date &gt; now() </if> <if test='req.leaseStatus == 2'> and end_date &lt; now() </if> <if test='req.searchValue != null and req.searchValue !=\"\"'> and (site_name like concat('%',#{req.searchValue},'%') or sn_code like concat('%',#{req.searchValue},'%')) </if> </script>")
    Page<RentalRoomDeviceRes> findRentalRoomDevice(RentalRoomDeviceReq req, Page page);


    @Select("<script> select count(1) from rental_device where is_delete = 0 and end_date > now() and consumer_id = #{consumerId} and device_id in <foreach collection=\"deviceId\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach>  <if test = 'roomId != null and !roomId.isEmpty()'> and room_id in <foreach collection=\"roomId\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach> </if> </script>")
    Integer findRentalRoomDeviceCount(RentalDeviceAddReq req);

    @Select("<script> select count(1) from rental_device where is_delete = 0 and consumer_id = #{consumerId} and device_id = #{deviceId}   <if test = 'roomId != null'> and room_id = #{roomId} </if> </script>")
    Integer findRentalDeviceRoomCount(@Param("consumerId") String consumerId, @Param("deviceId") String deviceId , @Param("roomId") String roomId);

    /**
     * 添加租赁信息
     */
//    @Insert("insert into rental_device(id,consumer_id,start_time,end_time)")
//    void addRentalDevice();

    @Update("update rental_device set is_delete = 1, end_date = now() where id = #{id}")
    void del(@Param("id") String id);
}
